"use strict";
cc._RF.push(module, '970a0iiYWRDJoQR1iVGJxmd', 'game');
// scripts/game.js

"use strict";

window.innerCellsList = [], //场景数字格子的数组
window.genreatingNumber = 0, //本次生成的数字格子的个数
window.putDownNumber = 0; //可放下的个数

window.cellColor = [{ "num": 2, "color": "#ffffff" }, { "num": 4, "color": "#12abf3" }, { "num": 8, "color": "#6af33f" }, { "num": 16, "color": "#f3fa3f" }, { "num": 32, "color": "#fc8f9f" }, { "num": 64, "color": "#66f71f" }, { "num": 128, "color": "#81f5ff" }], cc.Class({
    extends: cc.Component,

    properties: {

        cell: cc.Prefab,
        cellsNode: cc.Node,

        innercell: cc.Prefab,
        innercellInterface: cc.Node,

        qqq: cc.Node,
        aaa: cc.Node

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start: function start() {
        this.posId = [[0, 1, 2, 3], [4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27], [28, 29, 30, 31, 32], [33, 34, 35, 36]];

        //正六边形的边长
        this.sideLength = 56;
        this.innerHeight = this.sideLength / 2 * 1.7;
        this.gap = 10;
        this.cells = [];
        this.generateInnrecell = [];
        this.initPos = { x: -500, y: 0 };
        this.creatCells(); //生成棋盘

        //查找过的格子
        this.checkedCells = [];
        //准备合成的格子
        this.ready2CombineCells = [];

        this.screenH = cc.winSize.height; //屏幕的高
        this.screenW = cc.winSize.width;

        cc.director.getCollisionManager().enabled = true; //开启碰撞
        //cc.director.getCollisionManager().enabledDebugDraw = true;//显示碰撞区域
        // this.aaa.parent=this.cellsNode;//更换节点的父物体


        this.draggingType = []; //可拖动格子的种类

        this.generatePos = []; //可拖动格子的坐标
        this.draggingLatticePos(); //设置可拖动格子的坐标

        this.draggingInnercell = []; //可拖动的格子的数组

        //下方6个格子的顺序  左上 右上 左 右 左下 右下
        this.below6cellPos = [{ "i": 1, "j": 0 }, { "i": 1, "j": 1 }, { "i": 0, "j": -1 }, { "i": 0, "j": 1 }, { "i": -1, "j": -1 }, { "i": -1, "j": 0 }];
        //上方6个格子的顺序  左上 右上 左 右 左下 右下
        this.upper6cellPos = [{ "i": 1, "j": -1 }, { "i": 1, "j": 0 }, { "i": 0, "j": -1 }, { "i": 0, "j": 1 }, { "i": -1, "j": 0 }, { "i": -1, "j": 1 }];

        this.addEventHandler();

        this.ergodicWholeCells(); //bi
        this.generatecell(); //生成格子
    },
    addEventHandler: function addEventHandler() {
        var _this = this;

        //按下事件监听
        this.node.on('touchstart', function (event) {
            _this.startPoint = event.getLocation();
            _this.startPoint.x -= _this.screenW / 2;
            _this.startPoint.y -= _this.screenH / 2;

            // this.node.addChild(this.aaa);
            // console.log(this.startPoint);
            if (_this.startPoint.x >= _this.aaa.x - 150 && _this.startPoint.x <= _this.aaa.x + 150 && _this.startPoint.y >= _this.aaa.y - 150 && _this.startPoint.y <= _this.aaa.y + 150) {
                _this.IstoMove = true;
                //console.log("按下");
            }
        });
        //移动事件监听
        this.node.on('touchmove', function (event) {
            _this.touchMove(event);
        });
        //离开事件监听
        this.node.on('touchend', function (event) {
            // this.aaa.parent=this.cellsNode;//更换节点的父物体
            //  this.qqq.addChild(this.aaa);
            // this.aaa.x = this.qqq.x; this.aaa.y = this.qqq.y;
            _this.IstoMove = false;

            //生成的格子都可以放下
            if (putDownNumber == genreatingNumber) {
                for (var i = 0; i < _this.generateInnrecell.length; i++) {
                    var _generate = _this.generateInnrecell[i];
                    var _cell = _generate.getComponent('innercell');
                    // console.log("爱上降低" + _cell.collistionArrayI + "  " + _cell.collistionArrayJ);

                    var _num = _generate.getComponent('innercell').innerNumber;
                    _this.cells[_cell.collistionArrayI][_cell.collistionArrayJ].getComponent('cell').subObject(_num);
                }
                for (var _i2 = 0; _i2 < _this.generateInnrecell.length; _i2++) {
                    var _generate2 = _this.generateInnrecell[_i2];
                    var _cell2 = _generate2.getComponent('innercell');
                    // console.log("爱上降低" + _cell.collistionArrayI + "  " + _cell.collistionArrayJ);

                    var _num2 = _generate2.getComponent('innercell').innerNumber;
                    //this.findTheSurroundingGrid(_cell);
                    _this.ready2CombineCells = [];
                    _this.checkedCells = [];
                    _this.find(_cell2);
                    //this.cells[_cell.collistionArrayI][_cell.collistionArrayJ].getComponent('cell').subObject(_num);
                    console.log('可以合成的格子' + _i2, "   合成数字 " + _num2, _this.ready2CombineCells);
                }
                // console.log('可以合成的格子', this.ready2CombineCells);
                _this.ergodicWholeCells(); //遍历格子
                _this.generatecell(); //生成可拖动的格子
                // this.aaa.parent = this.innercellInterface;
                // this.aaa.x = 0; this.aaa.y = 0;
            }
            _this.aaa.x = _this.qqq.x;_this.aaa.y = _this.qqq.y;
            putDownNumber = 0; //以碰撞的个数
        });
        //离开屏幕事件
        this.node.on('touchcancel', function (event) {
            _this.aaa.x = _this.qqq.x;_this.aaa.y = _this.qqq.y;
            _this.IstoMove = false;
            putDownNumber = 0; //以碰撞的个数
        });
    },
    touchMove: function touchMove(event) {
        if (this.IstoMove == true) {
            //  console.log("移动");
            this.movePoint = event.getLocation();

            this.movePoint.x -= this.screenW / 2;
            this.movePoint.y -= this.screenH / 2;

            this.aaa.x = this.movePoint.x;
            this.aaa.y = this.movePoint.y;
        }
    },


    //根据id获取坐标位置
    getPositionById: function getPositionById(id) {
        var pos = {};
        for (var i = 0; i < this.posId.length; i++) {
            var arr = this.posId[i];
            for (var j = 0; j < arr.length; j++) {
                if (id == arr[j]) {
                    pos.x = Math.abs(3 - i) * (this.innerHeight + this.gap / 2) + this.initPos.x + j * (this.innerHeight * 2 + this.gap);
                    pos.y = this.initPos.y + (this.innerHeight + this.gap / 2) * 1.7 * (i - 3);
                    return pos;
                }
            }
        }
    },


    //创建棋盘
    creatCells: function creatCells() {
        innerCellsList = [];
        for (var i = 0; i < this.posId.length; i++) {
            var arr = this.posId[i];
            var _innercelllist = [];
            var _cell = [];
            for (var j = 0; j < arr.length; j++) {

                var pos = {};
                pos.x = Math.abs(3 - i) * (this.innerHeight + this.gap / 2) + this.initPos.x + j * (this.innerHeight * 2 + this.gap);
                pos.y = this.initPos.y + (this.innerHeight + this.gap / 2) * 1.7 * (i - 3);

                var cell = cc.instantiate(this.cell);
                cell.setPosition(pos.x, pos.y);
                cell.getComponent('cell').id = arr[j];
                cell.getComponent('cell').arrayI = i;
                cell.getComponent('cell').arrayJ = j;

                this.cellsNode.addChild(cell);
                _cell.push(cell);
                _innercelllist.push(null);
            }
            this.cells.push(_cell);
            innerCellsList.push(_innercelllist);
        }

        // let cellNum = 37;
        // for (let i = 0; i < cellNum; i++) {
        //     let cell = cc.instantiate(this.cell);
        //     let pos = this.getPositionById(i);
        //     cell.setPosition(pos.x, pos.y);
        //     cell.getComponent('cell').id=i;
        //     this.cellsNode.addChild(cell);
        //     this.cells.push(cell);
        // }
    },


    //可以放吗
    isPutable: function isPutable() {
        var bool = true;

        return bool;
    },

    //遍历整个棋盘
    ergodicWholeCells: function ergodicWholeCells() {
        this.draggingType = [];

        for (var i = 0; i < this.cells.length; i++) {
            var _cell = this.cells[i];
            for (var j = 0; j < _cell.length; j++) {
                // console.log(this.cells[i][j].getComponent('cell').IsSurePut);
                //格子可以放东西 是空的
                if (this.cells[i][j].getComponent('cell').IsSurePut == true) {
                    this.cellType(0);

                    if (i < Math.floor(this.cells.length / 2)) {
                        if (this.cells[i + 1] != null) {
                            if (this.cells[i + 1][j] != null) {
                                if (this.cells[i + 1][j].getComponent('cell').IsSurePut == true) {
                                    //左上角是空的
                                    this.cellType(1);
                                }
                            }
                            if (this.cells[i + 1][j + 1] != null) {
                                if (this.cells[i + 1][j + 1].getComponent('cell').IsSurePut == true) {
                                    //右上角是空的
                                    this.cellType(2);
                                }
                            }
                        }
                    } else {
                        if (this.cells[i + 1] != null) {
                            if (this.cells[i + 1][j - 1] != null) {
                                if (this.cells[i + 1][j - 1].getComponent('cell').IsSurePut == true) {
                                    //左上角是空的
                                    this.cellType(1);
                                }
                            }
                            if (this.cells[i + 1][j] != null) {
                                if (this.cells[i + 1][j].getComponent('cell').IsSurePut == true) {
                                    //右上角是空的
                                    this.cellType(2);
                                }
                            }
                        }
                    }
                    if (this.cells[i][j + 1] != null) {
                        if (this.cells[i][j + 1].getComponent('cell').IsSurePut == true) {
                            //右边是空的
                            this.cellType(3);
                        }
                    }
                }

                //////////////////////
            }
        }
        //console.log(this.draggingType);
        ///////////////////////
    },

    //格子状态   单个 single   左上Left upper   右上 Right upper  右侧  Right
    cellType: function cellType(_type) {
        for (var i = 0; i < this.draggingType.length; i++) {
            if (_type == this.draggingType[i]) {
                return;
            }
        }
        this.draggingType.push(_type);
    },

    //可拖动格子坐标
    draggingLatticePos: function draggingLatticePos() {
        var pos0 = {};
        pos0.x = 0;
        pos0.y = 0;
        //左上
        var pos1 = {};
        pos1.x = -1 * (this.innerHeight + this.gap / 2) + 0 * (this.innerHeight * 2 + this.gap);
        pos1.y = (this.innerHeight + this.gap / 2) * 1.7 * 1;
        //右上
        var pos2 = {};
        pos2.x = 1 * (this.innerHeight + this.gap / 2) + 0 * (this.innerHeight * 2 + this.gap);
        pos2.y = (this.innerHeight + this.gap / 2) * 1.7 * 1;

        //右
        var pos3 = {};
        pos3.x = 0 * (this.innerHeight + this.gap / 2) + 1 * (this.innerHeight * 2 + this.gap);
        pos3.y = (this.innerHeight + this.gap / 2) * 1.7 * 0;

        var pos = [pos1, pos2, pos3];

        var _type0 = [];
        _type0.push(pos0);
        this.generatePos.push(_type0);
        for (var i = 0; i < pos.length; i++) {
            var _pos1 = {};
            _pos1.x = 0 - pos[i].x / 2;
            _pos1.y = 0 - pos[i].y / 2;

            var _pos2 = {};
            _pos2.x = pos[i].x - pos[i].x / 2;
            _pos2.y = pos[i].y - pos[i].y / 2;

            var _type1 = [];
            _type1.push(_pos1, _pos2);
            this.generatePos.push(_type1);
        }

        // this.generatePos = [pos0,pos1, pos2, pos3];//可拖动的格子坐标的数组
    },


    //生成
    generatecell: function generatecell() {
        if (this.draggingType == null) return;
        // this.wallInterface.removeAllChildren();//清空子物体
        this.generateInnrecell = [];
        this.aaa.removeAllChildren();

        var _ty = this.draggingType[Math.floor(Math.random() * this.draggingType.length)]; //场景上能出现的位置情况

        var _POS = this.generatePos[_ty]; //相应的坐标数据
        for (var i = 0; i < _POS.length; i++) {
            var _innercell = cc.instantiate(this.innercell);

            //let zz = Math.ceil(Math.random() * 10);
            var zz = cellColor[Math.floor(Math.random() * cellColor.length)].num;
            _innercell.getComponent('innercell').innerNumber = zz;

            _innercell.setPosition(_POS[i].x, _POS[i].y);
            this.aaa.addChild(_innercell);
            this.generateInnrecell.push(_innercell);
        }

        // let xx = Math.floor(Math.random() * this.generatePos.length);
        // let _pos = this.generatePos[xx];
        // let _innercell2 = cc.instantiate(this.innercell);
        // let zz2 = Math.ceil(Math.random() * 10);
        // _innercell2.getComponent('innercell').innerNumber = zz2;
        // _innercell2.getComponent('innercell').zxzx = 2;
        // _innercell2.setPosition(_pos.x, _pos.y);
        // this.aaa.addChild(_innercell2);
        // this.generateInnrecell.push(_innercell2);

        putDownNumber = 0; //以碰撞的个数
        genreatingNumber = this.generateInnrecell.length; //需碰撞的个数
    },


    //找周围的六个格子
    findTheSurroundingGrid: function findTheSurroundingGrid(cell) {
        var _i = cell.collistionArrayI;
        var _j = cell.collistionArrayJ;
        var _number = cell.innerNumber;
        var array = [];
        var _6direction = void 0;
        if (_i < this.cells.length / 2) {
            _6direction = this.below6cellPos;
        } else {
            _6direction = this.upper6cellPos;
        }

        for (var i = 0; i < _6direction.length; i++) {

            if (this.cells[_i + _6direction[i].i] != null) {
                if (this.cells[_i + _6direction[i].i][_j + _6direction[i].j] != null) {
                    var _c = this.cells[_i + _6direction[i].i][_j + _6direction[i].j];
                    if (_c.getComponent('cell').number == _number) {
                        // this.cells[_i + _6direction[i].i][_j + _6direction[i].j].getComponent('cell').hideObject();

                        array.push(_c.getComponent('cell'));
                        //    let ll=this.cells[_i + _6direction[i].i][_j + _6direction[i].j].getComponent('cell').id;
                        //    checkedCells.push(ll);
                        //    this.findTheSurroundingGrid(_i + _6direction[i].i,_J + _6direction[i].j,_number);
                    }
                }
            }
        }

        return array;
    },
    isReady: function isReady(_cell) {
        for (var i = 0; i < this.ready2CombineCells.length; i++) {
            var cell = this.ready2CombineCells[i];
            if (_cell.id == cell.id) {
                return true;
            }
        }
        return false;
    },
    isChecked: function isChecked(_cell) {
        for (var i = 0; i < this.checkedCells.length; i++) {
            var cell = this.checkedCells[i];
            if (cell.id == _cell.id) {
                return true;
            }
        }
        return false;
    },
    find: function find(cell) {
        this.checkedCells.push(cell);
        var array = this.findTheSurroundingGrid(cell);
        for (var i = 0; i < array.length; i++) {
            var _cell = array[i];
            if (!this.isReady(_cell)) {
                this.ready2CombineCells.push(_cell);
            }
            if (!this.isChecked(_cell)) {
                this.find(_cell);
            }
        }
    }

    // update (dt) {},

});

cc._RF.pop();